package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private AttrValueService attrValueVoService;

    @Autowired
    private FacilityInfoService facilityInfoService;

    @Autowired
    private LabelInfoService labelInfoService;

    @Autowired
    private PaymentTypeService paymentTypeService;

    @Autowired
    private LeaseTermService leaseTermService;


    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;


    /**
     * 分页查询房间信息
     *
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> roomPageInfo(IPage<RoomItemVo> page, RoomQueryVo queryVo) {

        return roomInfoMapper.roomPageInfo(page, queryVo);
    }

    /**
     * 根据id查询详情  修改数据的回显页面展示
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        /*
        该接口的实现方法还是老样子
        1. 先获取房间基本信息
        2. 再获取房间对应的中间表相关的信息，都是需要自己手写sql语句
        3. 封装到详情vo中  批量添加
         */
        //先获取房间基本信息
        RoomInfo roomInfo = this.getById(id);
        //将基本信息封装到详情vo中
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        //获取房间对应的中间表相关的信息

        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        List<GraphVo> graphVoList = graphInfoService.listGraphVoByRoomId(id);
        roomDetailVo.setGraphVoList(graphVoList);
        List<AttrValueVo> attrValueVoList = attrValueVoService.listAttrValueByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        List<FacilityInfo> facilityInfoList = facilityInfoService.listFacilityByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        List<LabelInfo> labelInfoList = labelInfoService.listLabelByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);
        List<PaymentType> paymentTypeList = paymentTypeService.listPaymentByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        List<LeaseTerm> leaseTermList = leaseTermService.listLeaseByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);


        return roomDetailVo;
    }

    /**
     * 根据id删除房间信息
     *
     * @param id
     */
    @Override
    public void removRoomById(Long id) {
        //删除房间基本信息
        this.removeById(id);
        //删除房间对应的中间表数据
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
        roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getId, id));
        roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
    }

    /**
     * 保存或更新房间信息
     *
     * @param roomSubmitVo
     */

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        /*
        1.先获取房间的id，用于判断是增还是改
        2、如果是改的话，我们常用的方法是把原有的信息全部删除，（旧的信息保存在提交的vo中，有变化改新的，没变化用旧的） 然后再添加新的数据
        3、如果是增的话，直接保存即可
        4、从提交的vo中分别获取中间表的所有id，再new一个临时list 用于存放中间表数据（存放实体），遍历list，将所有的中间表信息存进去，最后把list用saveBatch方法批量保存
         */
        //先获取房间的id
        Long id = roomSubmitVo.getId();
        RoomInfo roomInfo = this.getById(id);
        //保存或更新房间基本信息
        this.saveOrUpdate(roomInfo);

        //判断房间是否存在
        if (id != null) {
            //房间存在，更新，直接删除房间所有的中间表数据，然后再添加新的数据
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        }

        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> grapfInfo = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graph = new GraphInfo();
            graph.setUrl(graphVo.getUrl());
            graph.setItemType(ItemType.ROOM);
            graph.setItemId(roomSubmitVo.getId());
            graph.setName(graphVo.getName());
            grapfInfo.add(graph);
        });
            graphInfoService.saveBatch(grapfInfo);


        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> attrValueList = new ArrayList<>();
        attrValueIds.forEach(attrValueId -> {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
            attrValueList.add(roomAttrValue);
        });
        roomAttrValueService.saveBatch(attrValueList);

        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
            roomFacilityList.add(roomFacility);
                });
        roomFacilityService.saveBatch(roomFacilityList);

        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabelList = new ArrayList<>();
        labelInfoIds.forEach(labelInfoId -> {
            RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
            roomLabelList.add(roomLabel);
        });
        roomLabelService.saveBatch(roomLabelList);

        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        paymentTypeIds.forEach(paymentTypeId -> {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
            roomPaymentTypeList.add(roomPaymentType);
        });
        roomPaymentTypeService.saveBatch(roomPaymentTypeList);

        List<Long> leaseTermIds =
                roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        leaseTermIds.forEach(leaseTermId -> {
            RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
            roomLeaseTermList.add(roomLeaseTerm);
        });
        roomLeaseTermService.saveBatch(roomLeaseTermList);

    }
}




